package com.xue.action;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xue.pojo.Ingredient;
import com.xue.service.IngredientService;

@Controller
@RequestMapping("/admin/ingredients")
public class ManageIngredientsController {

	private final IngredientService ingredientService;

	public ManageIngredientsController(IngredientService ingredientService) {
		this.ingredientService = ingredientService;
	}

	@GetMapping
	public String ingredientsAdmin(Model model) {
		model.addAttribute("ingredients", ingredientService.findAll());
		return "ingredientsAdmin";
	}

	@PostMapping
	public String addIngredient(Ingredient ingredient) {
		ingredientService.addIngredient(ingredient);
		return "redirect:/admin/ingredients";
	}

}
